package com.interview.thread.achieve;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


public class CallableTest03 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        new Thread().start();
        System.out.println("测试1");

        //创建MyCallable
        MyCallable myCallable = new MyCallable();
        //创建FutureTask ，传入myCallable
        FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
        //创建线程
        Thread thread = new Thread(futureTask, "A");
        //启动线程
        thread.start();
        System.out.println("测试2");
        Integer count = futureTask.get();
        System.out.println("总和为" + count);
    }


}

class MyCallable implements Callable<Integer> {

    @Override
    public Integer call() {
        int count = 0;
        for (int i = 0; i < 100; i++) {
            count += i;
        }
        System.out.println(count);
        return count;
    }
}
